﻿using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Test.Service;
using TestUnitTest.Base;
using TianShu;
using TianShu.Data;
using TianShu.Modularity;
using TianShu.MultiTenancy;
using TianShu.Threading;

namespace TestUnitTest
{
    [DependsOn(typeof(TianShuTestBaseModule),
        typeof(TestServiceModule))]
    public class TestUnitTestModule : AbpModule
    {
        public override void PreConfigureServices(ServiceConfigurationContext context)
        {
            //context.Services.AddSingleton<>
        }

        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            var services = context.Services;
            var configuration = context.Services.GetConfiguration();

            Configure<AbpDbConnectionOptions>(options =>
            {
                options.ConnectionStrings.Default = "Server=120.25.230.162;Port=3306;database=test1_liming;uid=devuser;pwd=Nb@devuser_P@ssw0rd;SslMode=None;Pooling=true;Max Pool Size=200;";
                options.ConnectionStrings.Provide = "mysql";
            });
            Configure<AbpTenantResolveOptions>(options =>
            {
                options.TenantResolvers.Add(new CurrentUserTenantResolveContributor());
            });
            //Configure<AttachmentConfigreOptions>(opt =>
            //{
            //    opt.SaceDire = "./";
            //});

        }

        public override void OnApplicationInitialization(ApplicationInitializationContext context)
        {
            SeedTestData(context);
        }

        private static void SeedTestData(ApplicationInitializationContext context)
        {
            AsyncHelper.RunSync(async () =>
            {
                using (var scope = context.ServiceProvider.CreateScope())
                {
                    await scope.ServiceProvider
                        .GetRequiredService<IDataSeeder>()
                        .SeedAsync();
                }
            });
        }
    }
}
